import {
	baseUrl
} from '@/api/base';
import request from '@/utils/request';

export function initOpenId() {
	uni.login({
		success: res => {
			request({
				url: baseUrl + '/mini/user/getSignUserByCode',
				method: 'post',
				data: {
					code: res.code
				}
			}).then(resp => {
				if (!resp.data.hasError) {
					if (resp.data.result) {
						let openId = resp.data.result.openId;
						getApp().globalData.openId = openId;
					}
				}
			})
		}
	})
}

export function updateUserInfo(userInfo) {
	let updateData = {
		openId: getApp().globalData.openId,
		criteria: {
			wxHeadUrl: userInfo.avatarUrl,
			wxUserName: userInfo.nickName
		}
	};
	request({
		url: baseUrl + '/mini/user/updateUserInfo',
		method: 'post',
		data: updateData
	}).then(resp => {
		if (!resp.data.hasError) {
			getApp().globalData.userInfo = resp.data.result;
		}
	})
}
